package com.test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Utils {

	private static final Log LOG = LogFactory.getLog(Utils.class);

	public static void unZip(Path path) {
		if (!Files.exists(path) || !Files.isRegularFile(path)) {
			return;
		}
		Path dirPath = path.getParent();
		try (ZipFile zfile = new ZipFile(path.toFile(), Charset.forName("GBK"))) {
			zfile.stream().forEach((entry) -> {
				Path filePath = dirPath.resolve(entry.getName());
				if (entry.isDirectory() && !Files.exists(filePath)) {
					filePath.toFile().mkdir();
				}
				try (BufferedInputStream bis = new BufferedInputStream(zfile.getInputStream(entry));
						BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath.toFile()))) {
					int read;
					byte[] buffer = new byte[1024 * 10];
					while ((read = bis.read(buffer, 0, buffer.length)) != -1) {
						bos.write(buffer, 0, read);
					}
					bos.flush();
				} catch (IOException e) {
					LOG.error(e);
					throw new RuntimeException(e);
				}
			});

		} catch (IOException e) {
			LOG.error(e);
			throw new RuntimeException(e);
		}
	}

	@Deprecated
	public static void unZip_old(Path path) {
		if (!Files.exists(path) || !Files.isRegularFile(path)) {
			return;
		}
		Path basePath = path.getParent();
		// 缓冲区大小
		int buffersize = 1024;
		int count = 0;
		// 缓冲区
		byte[] buffer = new byte[buffersize];
		try {
			// Zip文件
			ZipFile zfile = new ZipFile(path.toFile(), Charset.forName("GBK"));
			// 获取Zip包里的所有元素

			Enumeration<ZipEntry> zips = (Enumeration<ZipEntry>) zfile.entries();
			// 遍历Zip包里的所有元素
			while (zips.hasMoreElements()) {
				// 获取文件
				ZipEntry entry = zips.nextElement();
				// 文件名
				String name = entry.getName();
				// 解压后文件的全路径
				Path filePath = basePath.resolve(name);
				// 处理zip文件包含文件夹
				if (entry.isDirectory()) {
					if (!Files.exists(filePath)) {
						filePath.toFile().mkdir();
					}
					continue;
				}
				// 获取压缩文件输入流
				InputStream in = zfile.getInputStream(entry);
				// 带缓冲的输入流
				BufferedInputStream bis = new BufferedInputStream(in);
				// 带缓冲的输入流
				BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath.toFile()));
				// 从输入流中读取字节并写入输出流中
				while ((count = bis.read(buffer, 0, buffersize)) != -1) {
					bos.write(buffer, 0, count);
				}
				// 释放资源
				bos.flush();
				bos.close();
				bos.close();
			}
			// 关闭文件
			zfile.close();
		} catch (ZipException e) {
		} catch (IOException e) {
		}
	}
}
